package com.cskaoyan.servlet;

import com.cskaoyan.service.SystemConfigService;
import com.cskaoyan.service.SystemConfigServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author stone
 * @date 2023/03/20 15:19
 */
@WebServlet("/admin/config/*")
public class AdminConfigServlet extends WdBaseServlet{
    private SystemConfigService systemConfigService = new SystemConfigServiceImpl();
    @Override
    protected void handle(String operation, HttpServletRequest request, HttpServletResponse response) throws IOException {
        switch (operation) {
            case "/mall":
                mall(request, response);
                break;
        }
    }

    private void mall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            mallQuery(request, response);
        }else {
            //POST方法做更新
            mallModify(request, response);
        }
    }

    private void mallModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Map<String, String> data = JacksonUtil.read(jsonStr, Map.class);
        systemConfigService.updateConfig(data);
        ResponseUtil.responseOkJsonVo(response,null);
    }

    private void mallQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> data = systemConfigService.listMall();
        ResponseUtil.responseOkJsonVo(response,data);
    }
}
